import {createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/Login.vue'
import PersonnelDepartment from '../views/personnel/personnel-department.vue'
import PersonnelPermission from '../views/personnel/personnel-permission.vue'
import PersonnelRole from '../views/personnel/personnel-role.vue'
import PersonnelUser from '../views/personnel/personnel-user.vue'
import CooperateCompany from '../views/cooperate/cooperate-company.vue'
import InventoryList from '../views/inventory/inventory-inventoryList.vue'
import InventoryOutBoundApply from '../views/inventory/inventory-outBoundApply.vue'
import InventoryOutBoundLog from '../views/inventory/inventory-outBoundLog.vue'
import InventoryOutBoundLogAll from '../views/inventory/inventory-outBoundLogAll.vue'
import MyselfToDoItems from  '../views/myself/myself-toDoItems.vue'
import OrdersProcurement from '../views/orders/orders-procurement.vue'
import OrdersProcurementLogAll from '../views/orders/orders-procurementLogAll.vue'
import OrdersProcurementApply from '../views/orders/orders-procurementApply.vue'
import SalesLeaveBill from '../views/sales/sales-leaveBill.vue'
import SalesLeaveBillAll from '../views/sales/sales-leaveBillAll.vue'
import SalesLeaveApply from '../views/sales/sales-leaveApply.vue'
import SalesBill from '../views/sales/sales-salesBill.vue'
import StatsInformation from '../views/stats/stats-information.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: '/personnel/department',
        name: 'PersonnelDepartment',
        component: PersonnelDepartment
      },
      {
        path: '/personnel/permission',
        name: 'PersonnelPermission',
        component: PersonnelPermission
      },
      {
        path: '/personnel/role',
        name: 'PersonnelRole',
        component: PersonnelRole
      },
      {
        path: '/personnel/user',
        name: 'PersonnelUser',
        component: PersonnelUser
      },
      {
        path: '/cooperate/company',
        name: 'CooperateCompany',
        component: CooperateCompany
      },
      {
        path: '/inventory/inventoryList',
        name: 'InventoryList',
        component: InventoryList
      },
      {
        path: '/inventory/outBoundApply',
        name: 'InventoryOutBoundApply',
        component: InventoryOutBoundApply
      },
      {
        path: '/inventory/outBoundLog',
        name: 'InventoryOutBoundLog',
        component: InventoryOutBoundLog
      },
      {
        path: '/inventory/outBoundLogAll',
        name: 'InventoryOutBoundLogAll',
        component: InventoryOutBoundLogAll
      },
      {
        path: '/myself/toDoItems',
        name: 'MyselfToDoItems',
        component: MyselfToDoItems
      },
      {
        path: '/orders/procurement',
        name: 'OrdersProcurement',
        component: OrdersProcurement
      },
      {
        path: '/orders/procurementAll',
        name: 'OrdersProcurementLogAll',
        component: OrdersProcurementLogAll
      },
      {
        path: '/orders/procurementApply',
        name: 'OrdersProcurementApply',
        component: OrdersProcurementApply
      },
      {
        path: '/sales/leaveApply',
        name: 'SalesLeaveApply',
        component: SalesLeaveApply
      },
      {
        path: '/sales/leaveBill',
        name: 'SalesLeaveBill',
        component: SalesLeaveBill
      },
      {
        path: '/sales/leaveBillAll',
        name: 'SalesLeaveBillAll',
        component: SalesLeaveBillAll
      },
      {
        path: '/sales/salesBill',
        name: 'SalesBill',
        component: SalesBill
      },
      {
        path: '/stats/information',
        name: 'StatsInformation',
        component: StatsInformation
      },
    ]
  },
  {
    path: '/',
    name: 'Login',
    component: Login

  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

router.beforeEach(
    (to,from,next)=>{
      const loginRequire = to.matched.some((item)=>item.meta.loginRequire);
      if (loginRequire){
        const loginUser = sessionStorage.getItem("loginUser")
        if (!loginUser || JSON.stringify(loginUser) == '{}'){
          next("/")
        }else {
          next();
        }
      }else {
        next(); //放行
      }
    }
)

export default router
